"""Import record repository implementation."""

from __future__ import annotations

from uuid import UUID

from sqlalchemy.orm import Session

from ..models.import_record import ImportRecord
from ..repositories.base import Repository
from ..schemas.import_record import ImportRecordCreate, ImportRecordUpdate


class ImportRecordRepository(Repository[ImportRecord, ImportRecordCreate, ImportRecordUpdate]):
    """Repository for ImportRecord operations."""

    def __init__(self, session: Session) -> None:
        super().__init__(ImportRecord, session)

    def get_by_batch(self, batch_id: UUID, skip: int = 0, limit: int = 100) -> list[ImportRecord]:
        """Get import records by batch ID with pagination."""
        return (
            self.session.query(ImportRecord)
            .filter(ImportRecord.import_batch_id == batch_id)
            .offset(skip)
            .limit(limit)
            .all()
        )

    def get_by_status(self, batch_id: UUID, status: str) -> list[ImportRecord]:
        """Get records by batch and status."""
        return (
            self.session.query(ImportRecord)
            .filter(ImportRecord.import_batch_id == batch_id, ImportRecord.status == status)
            .all()
        )

    def get_pending_records(self, batch_id: UUID) -> list[ImportRecord]:
        """Get all pending records for a batch."""
        return self.get_by_status(batch_id, "pending")

    def count_by_status(self, batch_id: UUID, status: str) -> int:
        """Count records by batch and status."""
        return (
            self.session.query(ImportRecord)
            .filter(ImportRecord.import_batch_id == batch_id, ImportRecord.status == status)
            .count()
        )

    def bulk_update_status(self, record_ids: list[int], new_status: str) -> int:
        """Bulk update status for multiple records."""
        count = (
            self.session.query(ImportRecord)
            .filter(ImportRecord.id.in_(record_ids))
            .update({"status": new_status}, synchronize_session=False)
        )
        self.session.flush()
        return count
